<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

if (!function_exists('getTree1')) {
    /**
     * @Description: 无限极分类一
     * @Author: Yang
     * @param $data  数据库数据
     * @param int $parent_id   父级ID
     * @return array
     */
    function getTree1($data, $parent_id = 0)
    {
        $tree = array();
        foreach ($data as $k => $v) {
            if ($v["pid"] == $parent_id) {
                unset($data[$k]);
                if (!empty($data)) {
                    $children = getTree1($data, $v["id"]);
                    if (!empty($children)) {
                        $v["children"] = $children;
                    }
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }
}

if (!function_exists('getTree2')) {
    /**
     * @Description: 无限极分类二
     * @Author: Yang
     * @param $data   数据库数据
     * @param int $parent_id  父级ID
     * @param int $level  等级
     * @return array
     */
    function getTree2($data, $parent_id = 0, $level = 0)
    {
        static $tree = array();
        foreach ($data as $k => $v) {
            if ($v["pid"] == $parent_id) {
                $v["level"] = $level;
                $tree[] = $v;
                getTree2($data, $v["id"], $level + 1);
            }
        }
        return $tree;
    }
}

if (!function_exists('createNonceStr')) {
    /**
     * 生成指定长度的字符串
     * @url createNonceStr
     * @method GET
     * @param int $length 生成字符串长度
     * @return string $str 结果
     */
    function createNonceStr($length = 16)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}

if (!function_exists('createPassword')) {
    /**
     * 生成匹配密码
     * @url createPassword
     * @method GET
     * @param string $password 密码
     * @param string $passsalt 盐
     * @return string $str 生成的密码
     */
    function createPassword($password, $passsalt)
    {
        return md5(md5($password). $passsalt);
    }
}